package com.nervenets.template.controller.order.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.template.hibernate.domain.order.RechargeOrder;
import com.nervenets.template.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class RechargeOrderPagingParams extends EntityPagingParams<RechargeOrder> {
    @ApiModelProperty(value = "目标用户，不传不筛选")
    private long userId;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<RechargeOrder> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("createBy"), userId, () -> getBean(UserService.class).findOne(userId, "您查找的用户不存在") != null);
    }
}
